package com.kea.firsthorse.shared;

import com.google.gwt.regexp.shared.RegExp;

/**
 * @author Stephan Marc Hansen
 * <p>
 * FieldVerifier validates that the name the user enters is valid.
 * </p>
 * <p>
 * This class is in the <code>shared</code> package because we use it in both
 * the client code and on the server. On the client, we verify that the name is
 * valid before sending an RPC request so the user doesn't have to wait for a
 * network round trip to get feedback. On the server, we verify that the name is
 * correct to ensure that the input is correct regardless of where the RPC
 * originates.
 * </p>
 * <p>
 * When creating a class that is used on both the client and the server, be sure
 * that all code is translatable and does not use native JavaScript. Code that
 * is not translatable (such as code that interacts with a database or the file
 * system) cannot be compiled into client side JavaScript. Code that uses native
 * JavaScript (such as Widgets) cannot be run on the server.
 * </p>
 */
public class FieldVerifier {

	/**
	 * Verifies that the specified name is valid for our service.
	 * 
	 * In this example, we only require that the name is at least four
	 * characters. In your application, you can use more complex checks to
	 * ensure that usernames, passwords, email addresses, URLs, and other fields
	 * have the proper syntax.
	 * 
	 * @param name
	 *            the name to validate
	 * @return true if valid, false if invalid
	 */
	public static boolean isValidName(String name) {
		System.out.print("validname:");
		if (name == null) {
			System.out.println("false");
			return false;
		}
		//vi bruger gwts klasse til regulære udtryk, da javas ikke kan oversættes
		//til klientsidekode (i /shared)
		RegExp ip = RegExp.compile("^\\d*$");
		if (!ip.test(name)) {
			System.out.println("false uid not int");
			return false;
		}
		System.out.println(name.length() > 0);
		return name.length() > 0;
	}

	public static boolean isValidPassword(String password) {
		if (password == null) {
			return false;
		}
		return password.length() > 4;
	}

	/**
	 * @param pwhash
	 * @return true if hash is valid.
	 */
	public static boolean isValidPWHash(String pwhash) {
		if (pwhash == null) {
			return false;
		}
		return true;
	}
}
